var core = {}

core.relative_w = 75;
core.relative_h = 14;
core.$tree_node_elm_source = null;
core.$tree_node_dragging_elm = null;
core.work_panel_scope = {x1: 0, x2: 0, y1: 0, y2: 0};
core.checkDropScope = function (event) {
    if (event.clientX > core.work_panel_scope.x1 && event.clientX < core.work_panel_scope.x2
        && event.clientY > core.work_panel_scope.y1 && event.clientY < core.work_panel_scope.y2) {
        return true;
    }
    return false;
}

//辅助对齐 - 动态创建的对齐参照
window.ref_node_elm = null;
//辅助对齐 - 源算法组件
window.ref_node_elm_source = null;


$(function () {

    //加载右键菜单
    context.init({preventDoubleContext: false});

    $('.layout-left').height($(window).height() - 40);
    $('.layout-center').height($(window).height() - 40);
    $('.work_panel').height($(window).height() - 40);

    $('.ztree a').hover(function (event) {
        if ($(this).parent('li').has('ul')[0]) {
            return false;
        }
        // var msg = $(this).find('.node_name').text();
        // util.showFollowDialog(this, msg);
        $(this).addClass('ztree-node-a-hover');
        return false;
    }, function (event) {
        // util.hideFollowDialog();
        $(this).removeClass('ztree-node-a-hover');
        return false;
    });

    $('.ztree ul li a').poshytip({

        className: 'tip-twitter',

        showTimeout: 1,

        alignTo: 'target',

        alignX: 'center',

        offsetY: 5,

        allowTipHover: false,

        fade: true,

        slide: false,

        content: function (updateCallback) {
            return $(this).find('.node_name').text();
        }
    });

    $('.source-point:not(.jtk-dragging)').poshytip({

        className: 'tip-twitter',

        showTimeout: 1,

        alignTo: 'target',

        alignX: 'center',

        offsetY: 5,offsetX: -2,

        allowTipHover: false,

        fade: true,

        slide: false,

        liveEvents: true,

        content: function (updateCallback) {
            return '输入';
        }
    });

    $('.work_panel .pane-node-content').poshytip({

        className: 'tip-twitter',

        showTimeout: 300,

        alignTo: 'target',

        alignX: 'inner-left',

        alignY: 'bottom',

        offsetY: 9, offsetX: -12,

        allowTipHover: false,

        fade: true,

        slide: false,

        liveEvents: true,

        content: function (updateCallback) {
            var txt = '节点名称：' + $.trim($(this).text());
            txt += '<br/>算法名称：' + $(this).attr('name');
            return txt;
        }
    });

    $('.ztree a').bind('mousedown', function (event) {

        if ($(this).parent('li').has('ul')[0]) {
            return false;
        }

        var template = $('#node_template').html();
        template = template.replace(/\$\{nodeId\}/g, 'tempnode').replace(/\$\{icon\}/g, 'ico-click');
        template = template.replace(/\$\{text\}/g, $(this).find('.node_name').text());

        var nd = $(template);
        nd.css({left: event.clientX - core.relative_w, top: event.clientY - core.relative_h});
        $('body').append(nd);
        core.$tree_node_dragging_elm = nd;
        core.$tree_node_elm_source = $(event.target).parent();

        core.work_panel_scope.x1 = $('#work_panel').offset().left;
        core.work_panel_scope.x2 = $('#work_panel').offset().left + $('#work_panel').width();
        core.work_panel_scope.y1 = $('#work_panel').offset().top;
        core.work_panel_scope.y2 = $('#work_panel').offset().top + $('#work_panel').height();

    });

    $(document).on('mousedown', '.draggable', function (event) {

        if (event.button != 0) {
            return false;
        }

        window.ref_node_elm_source = this;

        draggable.addRefNode();

        draggable.start(event);
    });

    $(document).mousemove(function (event) {

        if (window.ref_node_elm) {
            var ui = {position: {}};
            ui.position.top = $(window.ref_node_elm).position().top;
            ui.position.left = $(window.ref_node_elm).position().left;
            draggable.drag(event, ui);
        } else if (core.$tree_node_dragging_elm) {
            if (core.$tree_node_dragging_elm.is(":animated")) {
                return false;
            }
            core.$tree_node_dragging_elm.css({
                left: event.clientX - core.relative_w,
                top: event.clientY - core.relative_h
            });
        }


    });

    $(document).mouseup(function (event) {

        if (window.ref_node_elm) {
            draggable.stop();
            $(window.ref_node_elm).remove();
            window.ref_node_elm = null;
            window.ref_node_elm_source = null
        } else if (core.$tree_node_dragging_elm) {
            if (core.checkDropScope(event)) {
                core.$tree_node_dragging_elm.remove()
                core.$tree_node_dragging_elm = null;
                var x = event.pageX - $('.layout-left').width() - $('.jtk-surface-canvas').position().left - core.relative_w;
                var y = event.pageY - $('.layout-left').offset().top - $('.jtk-surface-canvas').position().top - core.relative_h;
                var uid = jsPlumbUtil.uuid().replace(/\-/g,'');
                var node = window.addJsplumbNode(uid, core.$tree_node_elm_source.find('.node_name').text(), {x: x, y: y});
                window.addJsplumbPort(node, ['out1', 'out2'], 'output');
            } else {
                core.$tree_node_dragging_elm.animate({
                    left: core.$tree_node_elm_source.offset().left,
                    top: core.$tree_node_elm_source.offset().top
                }, 400, function () {
                    core.$tree_node_dragging_elm.remove()
                    core.$tree_node_dragging_elm = null;
                });
            }

        }


    });

    function keyUp(e) {
       var currKey = 0,e = e || event;
       currKey = e.keyCode || e.which || e.charCode;
       var keyName = String.fromCharCode(currKey);
       // alert("按键码: " + currKey + " 字符: " + keyName);
        if(currKey == 8){
            alert(11);
        }
   }
   // 监听删除
   document.onkeyup = keyUp;

    util.sucTip('成功');
   // util.warningTip('失败');

    var diag = new Dialog();
		diag.Width = 800;
		diag.Height = 560;
		diag.Title = "上传底图";
		diag.URL = 'http://baidu.com';
		diag.diagClose = function(fileOid,filePath){
			diag.close();

		};
		// diag.show();

});